package com.txsj.scan.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.adapter.AllocateHistoryAdapeter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.AllocationListBean;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.BaseResult;
import com.txsj.scan.presenter.AllocateHistoryPresenter;
import com.txsj.scan.refresh.XRefreshView;
import com.txsj.scan.utils.ToastUtils;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.AllocateHistoryIView;
import com.txsj.scan.widget.PinnedHeaderExpandableListView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 调拨单
 */
public class AllocateHistoryActivity extends BaseActivity<AllocateHistoryPresenter> implements AllocateHistoryIView, PinnedHeaderExpandableListView.OnHeaderUpdateListener  {




    PinnedHeaderExpandableListView aahExpandListView;

    XRefreshView xrefresh;

    Button btn_empty_reload;
    private int page = 1;
    private boolean isRefresh = false, isLoadMore = false;
    private List<AllocationListBean> list = new ArrayList<>();

    private AllocateHistoryAdapeter adapeter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allocate_history);
        initViews();
    }

    @Override
    protected AllocateHistoryPresenter providePresenter() {
        return new AllocateHistoryPresenter(this);
    }

    protected void initViews() {
        aahExpandListView = (PinnedHeaderExpandableListView)findViewById(R.id.aah_expandListView);
        xrefresh = (XRefreshView) findViewById(R.id.xrefresh);
        // 打开弹出框

        // 设置 xrefresh 是否可以下拉刷新
        xrefresh.setPullRefreshEnable(true);
        // 设置 xrefresh 是否可以上拉加载
        xrefresh.setPullLoadEnable(true);
        // 设置 xrefresh 是否可以自动刷新
        xrefresh.enableEmptyView(true);
        //当列表数据为空时 显示的View  的 方法
        initEmptyView(xrefresh);
        // xrefresh 的下拉刷新，上拉加载。
        xrefresh.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                //下拉刷新 修改值
                isRefresh = true;
                isLoadMore = false;
                page = 1;
                presenter.getAllocationList(0, page);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                //下拉加载 修改值
                isRefresh = false;
                isLoadMore = true;
                ++page;
                presenter.getAllocationList(0, page);
            }
        });
        //库存盘点历史 适配器 初始化
        adapeter = new AllocateHistoryAdapeter(this);
        aahExpandListView.setAdapter(adapeter);
        // 组装指示器
        aahExpandListView.setGroupIndicator(null);
        //adapeter 第二级列表 点击接口回调
        adapeter.setAllocateHistoryAdapeterinterface(new AllocateHistoryAdapeter.AllocateHistoryAdapeterinterface() {
            @Override
            public void setOnChildClickListener(int groupPosition, int childPosition) {
                Intent intent = new Intent(AllocateHistoryActivity.this, AllocationListActivity.class);
                intent.putExtra("Bill_id", list.get(groupPosition).getList().get(childPosition).getBill_id());
                startActivity(intent);
            }
        });
        xrefresh.startRefresh();

    }




    /**
     * 列表数据为空时 显示的View  的 方法
     */
    private void initEmptyView(final XRefreshView xRe) {
        View emptyView = View.inflate(this, R.layout.view_empty, null);
        btn_empty_reload = (Button) emptyView.findViewById(R.id.btn_empty_reload);
        xRe.setEmptyView(emptyView);
        btn_empty_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xRe.startRefresh();
            }
        });
    }

    // PinnedHeaderExpandableListView 头部展示
    @Override
    public View getPinnedHeader() {
        View headerView = (ViewGroup) getLayoutInflater().inflate(R.layout.item_ruku_group_explist_title, null);
        headerView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return headerView;
    }

    // 二级列表的头布局 默认的 view  ，  必须在这个方法赋值
    @Override
    public void updatePinnedHeader(View headerView, int firstVisibleGroupPos) {
        SimpleDateFormat simpleDateFormat0 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (list != null && list.size() > 0) {
                Date date = simpleDateFormat0.parse(list.get(firstVisibleGroupPos).getAdd_time());
                TextView tvhead = (TextView) headerView.findViewById(R.id.tv_date);
                tvhead.setText(simpleDateFormat1.format(date));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getAllocationListSuccess(List<AllocationListBean> dataList) {
        if (null == dataList) {
            return;
        }

        //关闭加载框
        hideLoading();
        // 上拉刷新
        if (isRefresh == true) {
            list.clear();
            list.addAll(dataList);
            // 关闭下拉刷新
            xrefresh.stopRefresh();
        } else {
            // 下拉加载
            // 关闭上拉加载
            xrefresh.stopLoadMore();
            if (dataList.size() <= 0) {
                // 返回的数据为空，
                t("已经全部加载完了！！！！！");
                return;
            }

            boolean isHave = false;
//            //本页面的数据
            if (list.size() > 0) {
                for (int i = list.size() - 1; i > 0; i--) {
                    if (list.get(i).getAdd_time().equals(dataList.get(0).getAdd_time())) {
                        List<AllocationListBean.ListBean> list1 = list.get(i).getList();
                        List<AllocationListBean.ListBean> list2 = dataList.get(0).getList();
                        list1.addAll(list2);
                        list.get(i).setList(list1);
                        isHave = true;
                        dataList.remove(0);
                        break;
                    }
                }
                if (isHave) {
                    list.addAll(dataList);
                } else {
                    list.addAll(dataList);
                }
            } else {
                list.addAll(dataList);
            }

        }
        //adapeter 的 setList方法
        adapeter.setList(list);
        //关闭列表数据为空时 显示的View  的 方法
        xrefresh.enableEmptyView(false);
        // PinnedHeaderExpandableListView 头布局  一级列表   的  回调
        aahExpandListView.setOnHeaderUpdateListener(this);
        //设置默认展开
        for (int i = 0; i < list.size(); i++) {
            aahExpandListView.expandGroup(i);
        }
    }

    //获取调拨单列表  失败
    @Override
    public void getAllocationListFail(String e) {
        xrefresh.stopRefresh();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200 && resultCode == 211) {//根据批次调拨成功后
            xrefresh.startRefresh();
            return;
        } else if (requestCode == 200 && resultCode == 212) {//根据标签调拨成功后
            xrefresh.startRefresh();
            return;
        }
    }


    @Override
    public Context context() {
        return this;
    }
}
